package com.token.iot.bratterycharging.gateway.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.outstanding.framework.base.OutstandingService;
import com.outstanding.framework.core.Logger;
import com.outstanding.framework.core.PendingException;
import com.outstanding.framework.log.LoggerFactory;
import com.token.iot.bratterycharging.gateway.Sender;
import com.token.iot.bratterycharging.gateway.SenderFactory;
import com.token.iot.bratterycharging.gateway.api.Ping;
import com.token.iot.bratterycharging.gateway.dao.ExceptionDAO;
import com.token.iot.bratterycharging.gateway.dao.PingDAO;
import com.token.iot.bratterycharging.gateway.server.SenderService;

/**
 * 业务指令服务
 * 
 * @author chenzhixuan
 *
 */
@Service
public class BratteryChargingCommandService extends OutstandingService {

	@Autowired
	private SenderService service;

	@Autowired
	private PingDAO dao;

	@Autowired
	private ExceptionDAO exceptionDao;

	protected static Logger loger = LoggerFactory.getLogger(BratteryChargingCommandService.class);

	public String getICCID(String imei) throws PendingException {
		try {
			Sender sender = SenderFactory.createSender("AID", imei);
			return (String) service.process(sender, imei, null);
		} catch (Exception e) {
			try {
				Throwable t = e.getCause();
				String context = e.getMessage();
				if (t != null) {
					context = " case:" + t.getMessage();
				}
				exceptionDao.insert(context);
			} catch (Exception ee) {
				loger.error("exceptionDao read has exception", ee);
			}
			throw e;
		}

	}

	public String getAddress(String imei) throws PendingException {
		try {
			Sender sender = SenderFactory.createSender("AAB", imei);
			String address = (String) service.process(sender, imei, null);
			return address;
		} catch (Exception e) {
			try {
				Throwable t = e.getCause();
				String context = e.getMessage();
				if (t != null) {
					context = " case:" + t.getMessage();
				}
				exceptionDao.insert(context);
			} catch (Exception ee) {
				loger.error("exceptionDao read has exception", ee);
			}
			throw e;
		}

	}

	public String restartDevice(String imei) throws PendingException {
		try {
			Sender sender = SenderFactory.createSender("ABT", imei);
			service.process(sender, imei, null);
			return "已重启";
		} catch (Exception e) {
			try {
				Throwable t = e.getCause();
				String context = e.getMessage();
				if (t != null) {
					context = " case:" + t.getMessage();
				}
				exceptionDao.insert(context);
			} catch (Exception ee) {
				loger.error("exceptionDao read has exception", ee);
			}
			throw e;
		}
	}

	public String getDeviceInfo(String imei) throws PendingException {
		try {
			Sender sender = SenderFactory.createSender("AAE", imei);
			String remark = (String) service.process(sender, imei, null);
			return remark;
		} catch (Exception e) {
			try {
				Throwable t = e.getCause();
				String context = e.getMessage();
				if (t != null) {
					context = " case:" + t.getMessage();
				}
				exceptionDao.insert(context);
			} catch (Exception ee) {
				loger.error("exceptionDao read has exception", ee);
			}
			throw e;
		}

	}

	public String getDeviceType(String imei) throws PendingException {
		try {
			Sender sender = SenderFactory.createSender("ASY", imei);
			String type = (String) service.process(sender, imei, null);
			return type;
		} catch (Exception e) {
			try {
				Throwable t = e.getCause();
				String context = e.getMessage();
				if (t != null) {
					context = " case:" + t.getMessage();
				}
				exceptionDao.insert(context);
			} catch (Exception ee) {
				loger.error("exceptionDao read has exception", ee);
			}
			throw e;
		}
	}

	public String getPingInfo(String imei) throws PendingException {
		try {
			Sender sender = SenderFactory.createSender("AXA", imei);
			String type = (String) service.process(sender, imei, null);
			return type;
		} catch (Exception e) {
			try {
				Throwable t = e.getCause();
				String context = e.getMessage();
				if (t != null) {
					context = " case:" + t.getMessage();
				}
				exceptionDao.insert(context);
			} catch (Exception ee) {
				loger.error("exceptionDao read has exception", ee);
			}
			throw e;
		}
	}

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

	public Ping selectPingByImei(String imei) throws PendingException {
		try {
			String ymd = sdf.format(new Date());
			Ping ping = dao.selectByImeiYmd(imei, ymd);
			if (ping != null) {
				ping.setYmd(ymd);
			}
			return ping;
		} catch (Exception e) {
			try {
				Throwable t = e.getCause();
				String context = e.getMessage();
				if (t != null) {
					context = " case:" + t.getMessage();
				}
				exceptionDao.insert(context);
			} catch (Exception ee) {
				loger.error("exceptionDao read has exception", ee);
			}
			throw e;
		}
	}

}
